package com.example.acer.myrecycle412zwf.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.acer.myrecycle412zwf.R;
import com.example.acer.myrecycle412zwf.bean.UserBean;
import com.example.acer.myrecycle412zwf.iface.LoginListener;
import com.example.acer.myrecycle412zwf.model.Loginmodel;
import com.example.acer.myrecycle412zwf.service.RegisterService;

public class LoginActivity extends AppCompatActivity implements LoginListener,View.OnClickListener{
    private EditText et_userName, et_password;
    private Button btn_login, btn_register;
    private CheckBox checkBox_remember;
    private Context context;
    private SharedPreferences preferences;
    final  static String PREF_NAME = "loginPref";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        context = LoginActivity.this;
        preferences = getSharedPreferences(PREF_NAME,MODE_PRIVATE);
        boolean isChecked = preferences.getBoolean("isChecked",false);
        if(isChecked){
            String userName = preferences.getString("userName","");
            String password = preferences.getString("password","");
            et_userName.setText(userName);
            et_password.setText(password);
            checkBox_remember.setChecked(isChecked);
        }
    }
    private void initView(){
        et_password=(EditText)findViewById(R.id.et_password);
        et_userName=(EditText)findViewById(R.id.et_username);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_register=(Button)findViewById(R.id.btn_register);
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        checkBox_remember=(CheckBox)findViewById(R.id.checkbox_remember);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.btn_login:
                Loginmodel mode = new Loginmodel();
                mode.login(et_userName.getText().toString(),
                        et_password.getText().toString(),this);
                break;
            case  R.id.btn_register:
                Intent intent = new Intent(context, RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void onResponse(UserBean userBean) {
        Toast.makeText(context,"ok",Toast.LENGTH_SHORT).show();
        //获取sessionid
        String sessionId = userBean.getSessionid();
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("sessionId",sessionId);
        editor.putBoolean("isChecked",checkBox_remember.isChecked());
        if(checkBox_remember.isChecked()){
            editor.putString("userName",et_userName.getText().toString());
            editor.putString("password",et_password.getText().toString());
        }
        editor.commit();
        Intent intent = new Intent(context,MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onFail(String msg) {

    }
}
